using System;
using System.Reflection;
namespace AppToolKit.Core.ObjectCopy
{
	public class PropertyCopyStrategy : IObjectCopyStrategy
	{
		public void Copy(object source, object dest, ICopyMappingStrategy mapStrategy, ICopyStrategy copyStrategy)
		{
			PropertyInfo[] properties = source.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
			PropertyInfo[] array = properties;
			for (int i = 0; i < array.Length; i++)
			{
				PropertyInfo propertyInfo = array[i];
				CopyStrategyHint hint = copyStrategy.GetHint(propertyInfo.Name);
				if (hint == CopyStrategyHint.Copy)
				{
					string text = Convert.ToString(mapStrategy.GetMapping(propertyInfo.Name));
					object value = propertyInfo.GetValue(source, null);
					PropertyInfo property = dest.GetType().GetProperty(text);
					if (property == null)
					{
						throw new ObjectCopyExecption(string.Format("实例{0}不存在属性{1}", dest, text));
					}
					if (!property.CanWrite)
					{
						throw new ObjectCopyExecption(string.Format("实例{0}属性{1}不能赋值", dest, text));
					}
					property.SetValue(dest, value, null);
				}
			}
		}
	}
}
